package com.tsh.chat.mapper;

import com.tsh.chat.domain.ChatHistory;
import com.tsh.chat.domain.Personality;
import com.tsh.chat.domain.PromptHistory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PersonalitySimulatorMapper {

    @Select("SELECT LAST_INSERT_ID()")
    int selectLastInsertId();
    /**
     * TABLE tsh_personality
     */
    @Insert("INSERT INTO tsh_personality(userId, description, achievement, relationship) VALUES(#{userId}, #{description}, #{achievement}, #{relationship})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertPersonality(Personality personality);

    @Delete("DELETE FROM tsh_personality WHERE id = #{id}")
    int deletePersonality(int id);

    @Update("UPDATE tsh_personality SET userId = #{userId}, description = #{description}, achievement = #{achievement}, relationship = #{relationship} WHERE id = #{id}")
    int updatePersonality(Personality personality);

    @Select("SELECT * FROM tsh_personality WHERE userId = #{userId}")
    List<Personality> selectPersonalityById(int userId);

    @Select("SELECT * FROM tsh_personality")
    List<Personality> selectAllPersonalities();

    /**
     * TABLE tsh_PromptHistory
     */
    @Insert("INSERT INTO tsh_prompt_history(userId, prompt) VALUES(#{userId}, #{prompt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertPromptHistory(PromptHistory promptHistory);

    @Delete("DELETE FROM tsh_prompt_history WHERE id = #{id}")
    int deletePromptHistoryById(int id);

    @Delete("DELETE FROM tsh_prompt_history WHERE userId = #{userId}")
    int deletePromptHistoryByUserId(int userId);

    @Update("UPDATE tsh_prompt_history SET userId = #{userId}, prompt = #{prompt} WHERE id = #{id}")
    int updatePromptHistory(PromptHistory promptHistory);

    @Select("SELECT * FROM tsh_prompt_history WHERE userId = #{userId}")
    List<PromptHistory> selectPromptHistoryByUserId(int userId);

    @Select("SELECT * FROM tsh_prompt_history")
    List<PromptHistory> selectAllPromptHistories();

    /**
     * TABLE tsh_chat_simulator_history
     */
    @Select("SELECT * FROM tsh_chat_simulator_history WHERE user_id = #{userId} ORDER BY id")
    List<ChatHistory> findChatHistoryByUserId(int userId);

    @Insert("INSERT INTO tsh_chat_simulator_history (user_id, role, content) VALUES (#{user_id}, #{role}, #{content})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertChatHistory(ChatHistory chatHistory);

    @Update("UPDATE tsh_chat_simulator_history SET role = #{role}, content = #{content} WHERE id = #{id}")
    int updateChatHistory(ChatHistory chatHistory);

    @Delete("DELETE FROM tsh_chat_simulator_history WHERE user_id = #{userId}")
    int deleteChatHistoryByUserId(int userId);

    @Delete("DELETE FROM tsh_chat_simulator_history")
    int deleteAllChatHistory();
}
